function Snake(el){
    this.el = el
    this.len = 20
    //蛇
    this.snakeObj = [
        {x:3,y:2,color:'red',el:null},
        {x:2,y:2,color:'blue',el:null},
        {x:1,y:2,color:'blue',el:null}
    ]
    this.direction = 'right'
    this.init()
}
Snake.prototype.remove = function(){
    for (let i = 0; i < this.snakeObj.length; i++) {
        if(this.snakeObj[i].el){
        this.el.removeChild(this.snakeObj[i].el)
        }
    }
}
Snake.prototype.init = function(){
    this.remove()
    for (let i = 0; i < this.snakeObj.length; i++) {
        //创建蛇标签对象
        let divObj = document.createElement('div')
        //设置宽高  坐标  颜色
        divObj.style.width = this.len +'px'
        divObj.style.height = this.len +'px'
        divObj.style.position = 'absolute'
        divObj.style.left = this.snakeObj[i].x*this.len+'px'
        divObj.style.top = this.snakeObj[i].y*this.len+'px'
        divObj.style.background = this.snakeObj[i].color
        //单独保存蛇节对象
        this.snakeObj[i].el = divObj
        //放到页面展示
        this.el.appendChild(divObj)
    }
}

Snake.prototype.move = function(){
    for (let i = this.snakeObj.length-1; i >0; i--) {
        this.snakeObj[i].x = this.snakeObj[i-1].x
        this.snakeObj[i].y = this.snakeObj[i-1].y
    }
    switch (this.direction) {
        case 'top':
            this.snakeObj[0].y -=1
            break;
        case 'bottom':
            this.snakeObj[0].y +=1
            break;
        case 'left':
            this.snakeObj[0].x -=1
            break;
        case 'right':
            this.snakeObj[0].x +=1
            break;
       
    }
    this.init()
}